import axios from 'axios'
import Vue from 'vue'

import store from '../store'
import { getToken,removeToken } from './auth'
import qs from "qs"
//import qs from "qs"
// 创建axios实例

const service = axios.create({
  baseURL: "http://www.jiatingyigou.com:3007", // api的base_url
  timeout: 15000   
         
})
// request拦截器
service.interceptors.request.use(config => {
  
  if (store.getters.token) {
    config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
    }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.status !==1) {
      return Promise.reject('error')
    } else {
      return response.data
    }
  },
  error => {
    console.log('err' + error)// for debug
  
    return Promise.reject(error)
  }
)

export default service



